package net.wofly.sale.order.service.impl;

import net.wofly.cashier.order.util.PayType;
import net.wofly.sale.order.domain.PayInfo;
import net.wofly.sale.order.repository.PayInfoRepository;
import net.wofly.sale.order.service.IPayInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.transaction.Transactional;
import java.util.List;

@Service("iPayInfoService")
@Transactional
public class PayInfoServiceImpl implements IPayInfoService {
    
    @Autowired
    PayInfoRepository payInfoRepository;
    
    @Override
    public PayInfo save(PayInfo entity) {
        return payInfoRepository.save(entity);
    }
    
    @Override
    public void delete(PayInfo entity) {
        payInfoRepository.delete(entity);
    }
    
    @Override
    public void delete(String key) {
        payInfoRepository.delete(key);
    }
    
    @Override
    public PayInfo findOne(String key) {
        return payInfoRepository.findOne(key);
    }
    
    @Override
    public List<PayInfo> save(List<PayInfo> entityList) {
        return (List<PayInfo>) payInfoRepository.save(entityList);
    }
    
    @Override
    public PayInfo findByOrderIDOrderByPayTimeDesc(String orderID) {
        List<PayInfo> payInfoList = payInfoRepository.findByOrderIDOrderByPayTimeDesc(orderID);
        if (!CollectionUtils.isEmpty(payInfoList)) {
            PayInfo payInfo = payInfoList.get(0);
            if (null != payInfo) {
                payInfo.setPayTypeName(PayType.getName(payInfo.getPayType()));
            }
            return payInfo;
        }
        return null;
    }
}
